
fn max(array: &[i32]) -> i32 {
    let mut max_index = 0;
    let mut i = 1;
    while i < array.len() {
        if array[i] > array[max_index] {
            max_index = i;
        }
        i += 1;
    }
    array[max_index]
}

struct Point<T> {
    x: T,
    y: T,
}

impl<T> Point<T> {
    fn x(&self) -> &T {
        &self.x
    }
}

struct Person {
    name: String,
    age: u8
}

struct A {
    name: String,
}

struct B {
    name: String,
}

trait Descriptive {
    fn describe(&self) -> String;
    
    //fn describe(&self) -> String{
    //    String::from("[Object]")
    //}
}

impl Descriptive for Person {
    fn describe(&self) -> String {
        format!("{} {}", self.name, self.age)
    }
}

impl Descriptive for A {
    fn describe(&self) -> String {
        format!("{}", self.name)
    }
}

impl Descriptive for B {
    fn describe(&self) -> String {
        format!("{}", self.name)
    }
}


fn output(object: impl Descriptive) {
    println!("{}", object.describe());
}

/*fn some_function(bool bl) -> impl Descriptive {
    if bl {
        return A {name: String::from("A")};
    } else {
        return B {name: String::from("B")};
    }
}*///错误

/*trait Comparable {
    fn compare(&self, object: &Self) -> i8;
}

fn max<T: Comparable>(array: &[T]) -> &T {
    let mut max_index = 0;
    let mut i = 1;
    while i < array.len() {
        if array[i].compare(&array[max_index]) > 0 {
            max_index = i;
        }
        i += 1;
    }
    &array[max_index]
}

impl Comparable for f64 {
    fn compare(&self, object: &f64) -> i8 {
        if &self > &object { 1 }
        else if &self == &object { 0 }
        else { -1 }
    }
}*/

fn main() {
    let a = [2, 4, 6, 3, 1];
    println!("max = {}", max(&a));
    
    let p = Point { x: 1, y: 2 };
    println!("p.x = {}", p.x());

    let cali = Person {
        name: String::from("Cali"),
        age: 24
    };
    println!("{}", cali.describe());

    output(cali)

    //let arr = [1.0, 3.0, 5.0, 4.0, 2.0];
    //println!("maximum of arr is {}", max(&arr));

    //some_function(true).describe()
    //some_function(false).describe()
}













































